package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.cache.utils.CacheUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.secure.TokenUser;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.cache.SellerCacheConstant;
import cn.signalpha.modules.shop.entity.SellerMenu;
import cn.signalpha.modules.shop.service.SellerMenuService;
import cn.signalpha.modules.shop.vo.CheckedTreeVO;
import cn.signalpha.modules.shop.vo.SellerGrantTreeVO;
import cn.signalpha.modules.shop.vo.SellerMenuVO;
import cn.signalpha.modules.shop.wrapper.SellerMenuWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/seller/menu")
@Api(value = "商户菜单表", tags = "商户菜单表")
public class SpiSellerMenuController extends BaseController {

	private final SellerMenuService sellerMenuService;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入menu")
	public R<SellerMenuVO> detail(SellerMenu sellerMenu) {
		SellerMenu detail = sellerMenuService.getOne(Condition.getQueryWrapper(sellerMenu));
		return R.data(SellerMenuWrapper.build().entityVO(detail));
	}

    @GetMapping("/list")
    @ApiOperation(value = "列表", notes = "")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
	})
    public R<List<SellerMenuVO>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		QueryWrapper<SellerMenu> queryWrapper = Condition.getQueryWrapper(params, SellerMenu.class);
		queryWrapper.lambda().orderByAsc(SellerMenu::getSort);
        return R.data(SellerMenuWrapper.build().listNodeVO(sellerMenuService.list(queryWrapper)));
    }

	@GetMapping("/lazy-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
	})
	@ApiOperation(value = "懒加载列表", notes = "传入menu")
	public R<List<SellerMenuVO>> lazyList(Long parentId, @ApiIgnore @RequestParam Map<String, Object> menu) {
		List<SellerMenuVO> list = sellerMenuService.lazyList(parentId, menu);
		return R.data(SellerMenuWrapper.build().listNodeLazyVO(list));
	}

	@GetMapping("/menu-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "菜单列表", notes = "传入menu")
	public R<List<SellerMenuVO>> menuList(@ApiIgnore @RequestParam Map<String, Object> menu) {
		List<SellerMenu> list = sellerMenuService.list(Condition.getQueryWrapper(menu, SellerMenu.class).lambda().eq(SellerMenu::getCategory, 1).orderByAsc(SellerMenu::getSort));
		return R.data(SellerMenuWrapper.build().listNodeVO(list));
	}

	@GetMapping("/lazy-menu-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "懒加载菜单列表", notes = "传入menu")
	public R<List<SellerMenuVO>> lazyMenuList(Long parentId, @ApiIgnore @RequestParam Map<String, Object> menu) {
		List<SellerMenuVO> list = sellerMenuService.lazyMenuList(parentId, menu);
		return R.data(SellerMenuWrapper.build().listNodeLazyVO(list));
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入menu")
	public R submit(@Valid @RequestBody SellerMenu sellerMenu) {
		if (sellerMenuService.submit(sellerMenu)) {
			CacheUtil.clear(SellerCacheConstant.MENU_CACHE);
			CacheUtil.clear(SellerCacheConstant.MENU_CACHE, Boolean.FALSE);
			// 返回懒加载树更新节点所需字段
			Kv kv = Kv.create().set("id", String.valueOf(sellerMenu.getId()));
			return R.data(kv);
		}
		return R.fail("操作失败");
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		CacheUtil.clear(SellerCacheConstant.MENU_CACHE);
		CacheUtil.clear(SellerCacheConstant.MENU_CACHE, Boolean.FALSE);
		return R.status(sellerMenuService.removeMenu(ids));
	}

	@GetMapping("/routes")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "前端菜单数据", notes = "前端菜单数据")
	public R<List<SellerMenuVO>> routes(TokenUser user) {
		return R.data(sellerMenuService.routes(user.getRoleId()));
	}

	@GetMapping("/buttons")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "前端按钮数据", notes = "前端按钮数据")
	public R<List<SellerMenuVO>> buttons(TokenUser user) {
		return R.data(sellerMenuService.buttons(user.getRoleId()));
	}

	@GetMapping("/grant-tree")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "权限分配树形结构", notes = "权限分配树形结构")
	public R<SellerGrantTreeVO> grantTree(TokenUser user) {
		SellerGrantTreeVO vo = new SellerGrantTreeVO();
		vo.setMenu(sellerMenuService.grantTree(user));
		return R.data(vo);
	}

	@GetMapping("/tree")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<SellerMenuVO>> tree() {
		List<SellerMenuVO> tree = sellerMenuService.tree();
		return R.data(tree);
	}

	@GetMapping("/role-tree-keys")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "角色所分配的树", notes = "角色所分配的树")
	public R<CheckedTreeVO> roleTreeKeys(String roleIds) {
		CheckedTreeVO vo = new CheckedTreeVO();
		vo.setMenu(sellerMenuService.roleTreeKeys(roleIds));
		return R.data(vo);
	}

	@GetMapping("auth-routes")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "菜单的角色权限")
	public R<List<Kv>> authRoutes(TokenUser user) {
		if (Func.isEmpty(user)) {
			return null;
		}
		return R.data(sellerMenuService.authRoutes(user));
	}

}
